package com.zslc.foreignoption.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;

public class ProcessUtils {
    private static final Logger log = LoggerFactory.getLogger(ProcessUtils.class);


    public static boolean execCommandNeedWaitFor(String tag, String command, boolean needLog) {
        long start = System.currentTimeMillis();
        int execRes = -9999;
        String result = null;
        String errRes = null;
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", command});
            result = org.springframework.util.StreamUtils.copyToString(p.getInputStream(), Charset.defaultCharset());
            errRes = org.springframework.util.StreamUtils.copyToString(p.getErrorStream(), Charset.defaultCharset());
            log.info("[execCommandNeedWaitFor] ffmpeg执行结果执行指令: {}, 标签: {}, 当前线程ID: {}", command, tag, Thread.currentThread().getId());
            closeProcess(p);
            execRes = p.waitFor();
        } catch (IOException e) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败,IO异常", e);
            return false;
        } catch (InterruptedException e2) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败", e2);
        }
        String appendInfo = execRes == 0 ? "info:" + result : "error:" + errRes;
        long end = System.currentTimeMillis();
        if (needLog) {
            log.info("[execCommandNeedWaitFor] {},执行结果：{}，time:{}ms,{}", tag, execRes, end - start, appendInfo);
            return true;
        }
        return true;
    }

    public static Integer execCommandNeedWaitForWithReturn(String command) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", command});
            log.info("[execCommandNeedWaitFor] 执行结果执行指令: {}", command);

            int execRes = p.waitFor();
            return execRes;
        } catch (IOException e) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败,IO异常", e);
            return null;
        } catch (Exception e2) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败", e2);
            return null;
        }finally {
            closeProcess(p);
        }
    }

    public static Integer execytdlpCommand(String command) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(new String[]{command});
//            log.info("[execCommandNeedWaitFor] 执行结果执行指令: {}", command);
            int execRes = p.waitFor();
            return execRes;
        } catch (IOException e) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败,IO异常", e);
            return null;
        } catch (Exception e2) {
            log.error("[execCommandNeedWaitFor] 执行命令后waitfor失败", e2);
            return null;
        }finally {
            closeProcess(p);
        }
    }

    public static void closeProcess(Process process) {
        try {
            process.getOutputStream().close();
            process.getInputStream().close();
            process.getErrorStream().close();
        } catch (IOException e) {
            log.error("[ProcessUtils] closeProcess: 关闭process失败", e);
        }
    }
}
